﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Sublimation  
{
    public class Slot :GameObject
    {
        #region attributes
        protected Texture2D rangeDisplay;
        protected Tower ownerTower;
        protected SlotKind kind;
        public SlotKind Kind
        {
            get
            {
                return kind;
            }
            set
            {
                kind = value;
            }
        }
        protected int production_cost;
        public int Production_cost
        {
            get
            {
                return production_cost;
            }
            set
            {
                production_cost = value;
            }
        }
        protected float orientation;
        public float Orientation
        {
            get
            {
                return orientation;
            }
            set
            {
                orientation = value;
            }
        }
        protected float angle;
        public float Angle
        {
            get
            {
                return angle;
            }
            set
            {
                angle = value;
            }
        }
        protected float range;
        public float Range
        {
            get
            {
                return range;
            }
            set
            {
                range = value;
            }
        }
        #endregion


        public Slot(WorldProperties worldProperties, SlotKind kind, Tower ownerTower)
            : base(worldProperties)
        {
            LoadContent();
            this.ownerTower = ownerTower;
            this.worldProperties = worldProperties;
            XmlSerializer s = new XmlSerializer(typeof(SlotProperties));
            SlotProperties slot;
            TextReader r ;
            slot = new SlotProperties();
            //TextWriter w = new StreamWriter("Resources/XML/slots/energy.xml");
            //s.Serialize(w, slot);
            //w.Close();
            switch (kind)
            {
                case SlotKind.Energy_transmition:
                    r = new StreamReader("Resources/XML/slots/energy.xml");
                    break;
                case SlotKind.Base_weapon:
                    r = new StreamReader("Resources/XML/slots/weapon.xml");
                    break;
                default:
                    r = null;
                    break;
            }
            
            if (r != null)
            {
                slot = (SlotProperties)s.Deserialize(r);
                r.Close();
                kind = slot.Kind;
                orientation = slot.Orientation;
                production_cost = slot.Production_cost;
                angle = slot.Angle;
                Range = slot.Range;
            }

            // make sure it loads and draws
            Scale = 1.0f;
            DrawOrder = 51;
            UpdateOrder = 50;
            Visible = true;
            Enabled = true;

            // align to parent
            //TODO: make this line up with the slot position - some way of defining the origin (anchor point)
            Origin = Vector2.Zero;
            //this.Scale = 1.0f / SPRITE_SCALE_TO_ONE;
            //ScaleWithinObject(parent, ENERGY_SCALE_TO_PARENT);
            this.Velocity = Vector2.Zero;
            CentreOnObject(ownerTower);
        }
        protected override void LoadContent()
        {
            base.LoadContent();
            Sprite = Game.Content.Load<Texture2D>("empty");
            rangeDisplay = Game.Content.Load<Texture2D>("Sprites\\range");
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            // mirror the parents movements
            CentreOnObject(ownerTower);
            AlignOriginWithObject(ownerTower);
            //this.Rotation = ownerTower.Rotation;

           // ownerTower -= production_cost;
        }

        public override void Draw(GameTime gameTime)
        {
            
            base.Draw(gameTime);
            if (ownerTower.Mouving)
            {
                spriteBatch.Begin();
                spriteBatch.Draw(rangeDisplay, new Rectangle((int)ownerTower.Position.X, (int)ownerTower.Position.Y, (int)range, (int)(2 * range * angle / MathHelper.PiOver4)), null, Color.White, ownerTower.Rotation + Orientation, new Vector2(0, 500), SpriteEffects.None, 1);
                spriteBatch.End();
            }
        }
    }
}
